package lmh.example.exam3_9;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bt_get;
    private Button bt_post;
    private TextView tv_show;
    private Handler handler;      //定义一个android.os.Handler对象
    private String result = "";   //请求结果字符串

    final OkHttpClient client = new OkHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt_get = (Button) findViewById(R.id.bt_get);
        bt_post = (Button) findViewById(R.id.bt_post);

        bt_get.setOnClickListener(this);
        bt_post.setOnClickListener(this);

        tv_show = (TextView) findViewById(R.id.tv_show);

        handler = new Handler() {   //将服务器中的数据，显示在UI界面中
            @Override
            public void handleMessage(Message msg) {
                tv_show.setText(result);    // 显示回应信息
                super.handleMessage(msg);
            }
        };
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_get:
                getRequest();
                break;

            case R.id.bt_post:
                postRequest();
                break;

        }
    }

    // 点击“GET请求”按钮调用该方法
    private void getRequest() {

        final Request request = new Request.Builder()
                .get()
                .url("https://www.baidu.com")
                .build();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Response response = null;
                try {
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        result = response.body().string();
                        Message m = handler.obtainMessage(); // 获取一个Message
                        handler.sendMessage(m);             // 发送消息
                    } else {
                        throw new IOException("Unexpected code " + response);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    // 点击“POST请求”按钮调用该方法
    private void postRequest() {

        RequestBody formBody = new FormBody.Builder()
                .build();
        final Request request = new Request.Builder()
                .url("https://www.sina.com.cn/")
//                .get()
                .post(formBody)
                .build();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Response response = null;
                try {
                    response = client.newCall(request).execute();
                    result = response.body().string();
                    Message m = handler.obtainMessage(); // 获取一个Message
                    handler.sendMessage(m);             // 发送消息
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}